# Import the required pacakges
import matplotlib.pyplot as plt
import seaborn as sns
import plotly as pl
import plotly.express as px
pl.offline.init_notebook_mode()
Matplotlib
This is a Python library for creating static, interactive, and animated visualizations in a variety of formats. Learn More
Below a example of matplotlib plotting fuel price for 5 months (August 2023 - December 2023).
| Month | Value |
|---|---|
| August 2023 | 170.6 |
| September 2023 | 168.3 |
| October 2023 | 157.4 |
| November 2023 | 152.2 |
| December 2023 | 145.4 |
# Sample data
categories = ['August 2023', 'September 2023', 'October 2023', 'November 2023','December 2023']
values = [170.6, 168.3, 157.4, 152.2, 145.4]
# Set the graph size
plt.figure(figsize=(10, 6))
# Plot the graph
plt.plot(categories, values, color='skyblue')
# Add labels and title
plt.xlabel('Month')
plt.ylabel('Price')
plt.title('Fuel Price for last 5 months 2023')
# Display the plot
plt.show()
Seaborn
This is a library for making statistical graphics in Python. This is build on top of matplotlib package working closely with pandas data structures. Learn More
Below is an example for seaborn using scatter plot showing the realationship between horsepower and mills per gallon, it also includes the country of origin using different color and the size of the points representing the weight of the car.
# Load the "mpg" dataset
mpg = sns.load_dataset("mpg")
# Set Seaborn style
sns.set_theme(style="whitegrid")
# Create a scatter plot of horsepower vs. miles per gallon
sns.scatterplot(x="horsepower", y="mpg",
hue="origin", size="weight",
sizes=(20, 200), palette="muted",
data=mpg)
# Add labels and title
plt.xlabel('Horsepower')
plt.ylabel('Miles per Gallon')
plt.title('Scatter Plot of Horsepower vs. MPG')
# Show the plot
plt.show()
Plotly
This is an interactive, open-source, and browser-based graphing library. It offers Python-based charting, powered by plotly. js. Learn More
Below is an example of plotly where each row represents the availability of car-sharing services near the centroid of a zone in Montreal over a month-long period.
# Load a built-in dataset from Plotly Express (cars dataset)
df = px.data.carshare()
fig = px.scatter_mapbox(df, lat="centroid_lat", lon="centroid_lon", color="peak_hour", size="car_hours",
color_continuous_scale=px.colors.cyclical.IceFire, size_max=15, zoom=10,
mapbox_style="carto-positron")
fig.show()